package priv.mill.util;



import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**   
 *
 * @author Giomks
 * @version 1.0 2017-11-13 
 */
public class RedisAPI {
	private static Jedis jedis;
    private static JedisPool pool = null;
   private static String serverIp = RedisProper.getString("serverIp");
    private static int port = Integer.parseInt(RedisProper.getString("port"));
    private static int maxTotal = Integer.parseInt(RedisProper.getString("maxTotal"));
    private static int maxIdle = Integer.parseInt(RedisProper.getString("maxIdle"));
    private static int maxWaitMillis =  Integer.parseInt(RedisProper.getString("maxWaitMillis"));
    private static boolean testOnBorrow =Boolean.getBoolean(RedisProper.getString("testOnBorrow"));

	/**
     * 
     * 
     * @param ip
     * @param port
     * @return JedisPool
     */
	public static JedisPool getPool() {
		
        if (pool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(maxTotal);
            config.setMaxIdle(maxIdle);
            config.setMaxWaitMillis(maxWaitMillis);
            config.setTestOnBorrow(testOnBorrow);
            pool = new JedisPool(config, serverIp,port);
        }
        return pool;
    }
    
    /**
     * 
     * @param key
     * @return
     */
    public static String set(String key,String value){
         
        JedisPool pool = null;
        Jedis jedis = null;
        String set=null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            set = jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } 
         
        return set;
    }
    
    
    /**
     * 
     * @param key
     * @return
     */
    public static String setPerson(byte[] key,byte[] value){
         
        JedisPool pool = null;
        Jedis jedis = null;
        String set=null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            set = jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } 
         
        return set;
    }
     
    /**
     * 
     * @param key
     * @return
     */
    public static String get(String key){
        String value = null;
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
//            pool.close();
            e.printStackTrace();
        } 
         
        return value;
    }
    
    /**
     * 
     * @param key
     * @return
     */
    public static byte[] getPerson(String key){
    	byte[] value = null;
    	JedisPool pool = null;
    	Jedis jedis = null;
    	try {
    		pool = getPool();
    		jedis = pool.getResource();
    		value = jedis.get(key.getBytes());
    	} catch (Exception e) {
    		e.printStackTrace();
    	} 
    	
    	return value;
    }
    
    
}

